package com.itheima.interceptor;

import cn.hutool.core.util.StrUtil;
import com.itheima.utils.LoginJWTkey;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author:上官徵羽
 * @Date: 2025/07/17 16:15
 * @Description TODO 今日内容:
 */
@Component
public class AuthorizedInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("AuthorizedInterceptor preHandle");
        //获取ToKen
        String token = request.getHeader("token");
        //非空校验
        if (StrUtil.isBlank(token )){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
        try {
            Claims claims = LoginJWTkey.parsingEmpJWT(token);
            Object id = claims.get("id");
            System.out.println(id);
            return true;
        }catch (Exception a){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
    }
}
